<?php
/*
+--------------------------------------------------------------------------
|   AffiliStore 2
|   ========================================
|   Web: http://www.affilistore.com
|   Email: admin (at) affilistore (dot) com
|	License Type: AffiliStore 2 is NOT open source software and limitations apply 
|   Licence Info: Visit AffiliStore website and click on 'Licence'
+--------------------------------------------------------------------------
*/
if (eregi(".inc.php",$HTTP_SERVER_VARS['PHP_SELF']) || eregi(".inc.php",$_SERVER['PHP_SELF'])) {
	echo "<html>\r\n<head>\r\n<title>Forbidden 403</title>\r\n</head>\r\n<body><h3>Forbidden 403</h3>\r\nThe document you are requesting is forbidden.\r\n</body>\r\n</html>";
	exit;
}

// get titles
$getTitles = mysql_query(sprintf("SELECT * FROM affiliSt_config WHERE name = %s",
				quote_smart('feedMemory'.$_GET['proddb'])));
$allTitles = mysql_fetch_assoc($getTitles);
$listTitles = explode("-:-", $allTitles['value']);
		
// get listing options
$getListOptions = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'prodSingle'");
$listOptions = mysql_fetch_assoc($getListOptions);
$prodOptions = explode(":", $listOptions['value']);

$box_content = new XTemplate ("skins/".$configSkin['value']."/styleTemplates/singleproducts.tpl");

if ($row_product['dbProdID'] != NULL) {

        do {
		
					$box_content->assign("GOLINK",$installDir['value'].'go.php?proddb='.$row_product['prodDB'].'&amp;l='.$row_product['dbProdID']);
					$box_content->assign("PRODLINK",$row_product['dbProdID']);
					if ($navOpt[7] == 1) {
					// html perma
					$box_content->assign("IMAGELINK",$installDir['value'].'img/'.$row_product['prodDB'].'/'.$row_product['dbProdID'].'/1.jpg');
					} else {
					// php link
					$box_content->assign("IMAGELINK",$row_product['prodImageURL']);
					}
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[21];
						} else {
						$theTitle = '';
						}
					$box_content->assign("NAME",$theTitle.$row_product['prodName']);
					
					
					if ($prodOptions[0] == 1 && $row_product['merchant'] != '') {
					$box_content->assign("MERCHANT",'<span class="singleProdMerchant">'.$row_product['merchant'].'</span><br />');
					}
					if ($prodOptions[1] == 1 && $row_product['merchantProdID'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[19];
						} else {
						$theTitle = '';
						}
					$box_content->assign("PRODID", '<span class="singleProdID">'.$theTitle.$row_product['merchantProdID'].'</span><br />');
					}
					if ($prodOptions[2] == 1 && $row_product['prodCategory'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[20];
						} else {
						$theTitle = '';
						}
					$box_content->assign("PRODCAT",'<span class="singleProdCategory">'.$theTitle.$row_product['prodCategory'].'</span><br />');
					}
					if ($prodOptions[3] == 1 && $row_product['prodBrand'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[22];
						} else {
						$theTitle = '';
						}
					$box_content->assign("BRAND",'<span class="singleProdBrand">'.$theTitle.$row_product['prodBrand'].'</span><br />');
					}
					if ($prodOptions[4] == 1 && $row_product['prodPromoText'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[24];
						} else {
						$theTitle = '';
						}
					$box_content->assign("PROMO",'<span class="singleProdPromo">'.$theTitle.$row_product['prodPromoText'].'</span><br />');
					}
					if ($prodOptions[5] == 1 && $row_product['prodDescription'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[23];
						} else {
						$theTitle = '';
						}
					$box_content->assign("DESCRIPTION",'<span class="singleProdDescription">'.$theTitle.$row_product['prodDescription'].'</span><br />');
					}
					if ($prodOptions[6] == 1 && $row_product['prodPrice'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[27];
						} else {
						$theTitle = '';
						}
						if ($row_product['prodPrice'] == 0.00) {
						$box_content->assign("PRICE",'<span class="singleProdPrice">'.$theTitle.'N/A</span><br />');
						} else {
						$box_content->assign("PRICE",'<span class="singleProdPrice">'.$theTitle.$row_product['prodCurrency'].$row_product['prodPrice'].'</span><br />');
						}
					}
					if ($prodOptions[7] == 1 && $row_product['extraFieldA'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[28];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDA",'<span class="singleProdDescription">'.$theTitle.$row_product['extraFieldA'].'</span><br />');
					}
					if ($prodOptions[8] == 1 && $row_product['extraFieldB'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[29];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDB",'<span class="singleProdDescription">'.$theTitle.$row_product['extraFieldB'].'</span><br />');
					}
					if ($prodOptions[9] == 1 && $row_product['extraFieldC'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[30];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDC",'<span class="singleProdDescription">'.$theTitle.$row_product['extraFieldC'].'</span><br />');
					}
					if ($prodOptions[10] == 1 && $row_product['extraFieldD'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[31];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDD",'<span class="singleProdDescription">'.$theTitle.$row_product['extraFieldD'].'</span><br />');
					}
					if ($prodOptions[11] == 1 && $row_product['extraFieldE'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[32];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDE",'<span class="singleProdDescription">'.$theTitle.$row_product['extraFieldE'].'</span><br />');
					}
					if ($prodOptions[13] == 1 && $listTitles[33] != '') {
					$box_content->assign("MERCHANTLOGO",'<a href="'.$installDir['value'].'go.php?proddb='.$row_product['prodDB'].'&amp;l='.$row_product['dbProdID'].'" rel="nofollow" target="_blank"><img src="'.$installDir['value'].$listTitles[33].'" alt="'.$row_product['merchant'].'" style="padding:0px; margin:0px" border="0" /></a><br />');
					}
					
					if ($navOpt[13] == 1 && (($prodOptions[13] == 1 && $listTitles[33] != '') || ($prodOptions[0] == 1 && $row_product['merchant'] != ''))) {
					// merchant comments
					$getMerchantComments = mysql_query("SELECT * FROM affiliSt_comments WHERE attachedID = '".urltxt($row_product['merchant'])."' AND approve = '1' AND section = 'stores' ORDER BY date DESC");
					$merchantComments = mysql_fetch_assoc($getMerchantComments);
					// total comments
					$sql = "SELECT count(*) as numRows FROM affiliSt_comments WHERE attachedID = '".urltxt($row_product['merchant'])."' AND approve = '1' AND section = 'stores'";
					$query = mysql_query($sql);
					$aRow = mysql_fetch_array($query);
					//echo $aRow[numRows]
					
					$totalMerchantRating = 0;
						do {
						$totalMerchantRating = $totalMerchantRating + $merchantComments['rating'];
						} while ($merchantComments = mysql_fetch_assoc($getMerchantComments));
					
						if ($aRow[numRows] != 0) {
						$merchantRating = $totalMerchantRating / $aRow[numRows];
						$merchantRating = round($merchantRating);
						}
					
						if ($navOpt[7] == 1) {
						// html perma
						$merchantLink = $installDir['value'].'stores/'.urltxt($row_product['merchant']).'.html';
						} else {
						// php link
						$merchantLink = $installDir['value'].'index.php?case=stores&merchant='.urltxt($row_product['merchant']);
						}
						if ($aRow[numRows] == 0) {
						$box_content->assign("MERCHANTRATING",'<a href="'.$merchantLink.'">Review this store</a><br />');
						} else {
						$box_content->assign("MERCHANTRATING",'<img src="'.$installDir['value'].'skins/'.$configSkin['value'].'/styleImages/'.$merchantRating.'tic.gif" width="84" height="15" style="padding:0px; margin:0px" alt="Rating: '.$merchantRating.'" /><br />(<a href="'.$merchantLink.'">'.$aRow[numRows].' reviews</a>)<br />');
						}
					}

			// cookie for 5 recently viewed products and link for saved search for userhistory.inc.php
			if ($navOpt[7] == 1) {
			// html perma
			$recentProdLink = $installDir['value'].'product/'.strip_tags($_GET['proddb']).'/'.strip_tags($_GET['pid']).'/'.urltxt($row_product['prodName']).'.html-:-'.$row_product["prodName"];
			$savelink = $installDir['value'].'saveproduct/'.strip_tags($_GET['proddb']).'/'.strip_tags($_GET['pid']).'/'.urltxt($row_product['prodName']).'.html';
			} else {
			// php link
			$recentProdLink = $installDir['value'].'index.php?case=product&proddb='.strip_tags($_GET['proddb']).'&pid='.strip_tags($_GET['pid']).'-:-'.$row_product["prodName"];
			$savelink = $installDir['value'].'index.php?case=product&proddb='.strip_tags($_GET['proddb']).'&pid='.strip_tags($_GET['pid']).'&savelink=true';
			}
			// set recently viewed cookie
			if (!isset($_COOKIE['recentlyviewed'])) {
			setcookie("recentlyviewed[0]", $recentProdLink, time()+(60*60*24*365*10), "/", $_SERVER['SERVER_NAME']);
			} else {
				for ($vh=0;$vh<4;$vh++) {
					if (isset($_COOKIE['recentlyviewed'][$vh]) && ($recentProdLink != $_COOKIE['recentlyviewed'][0] && $recentProdLink != $_COOKIE['recentlyviewed'][1] && $recentProdLink != $_COOKIE['recentlyviewed'][2] && $recentProdLink != $_COOKIE['recentlyviewed'][3] && $recentProdLink != $_COOKIE['recentlyviewed'][4])) {
					setcookie("recentlyviewed[".($vh+1)."]", $_COOKIE['recentlyviewed'][$vh], time()+(60*60*24*365*10), "/", $_SERVER['SERVER_NAME']);
					setcookie("recentlyviewed[0]", $recentProdLink, time()+(60*60*24*365*10), "/", $_SERVER['SERVER_NAME']);
					}
				}
			}
			// set saved list cookie
			if (isset($_GET['savelink'])) {
				if (!isset($_COOKIE['savedlist'])) {
				setcookie("savedlist[0]", $recentProdLink, time()+(60*60*24*365*10), "/", $_SERVER['SERVER_NAME']);
				} else {
					for ($vh=0;$vh<4;$vh++) {
						if (isset($_COOKIE['savedlist'][$vh]) && ($recentProdLink != $_COOKIE['savedlist'][0] && $recentProdLink != $_COOKIE['savedlist'][1] && $recentProdLink != $_COOKIE['savedlist'][2] && $recentProdLink != $_COOKIE['savedlist'][3] && $recentProdLink != $_COOKIE['savedlist'][4])) {
						setcookie("savedlist[".($vh+1)."]", $_COOKIE['savedlist'][$vh], time()+(60*60*24*365*10), "/", $_SERVER['SERVER_NAME']);
						setcookie("savedlist[0]", $recentProdLink, time()+(60*60*24*365*10), "/", $_SERVER['SERVER_NAME']);
						}
					}
				}
			}
					$box_content->assign("SAVELINK",'<a href="'.$savelink.'" rel="nofollow">+ Add to my saved list</a><br />');
					$box_content->parse("singleprod.li");
			
		
        } while ($row_product = mysql_fetch_assoc($product));

$box_content->assign("SKIN",$configSkin['value']);
$box_content->parse("singleprod");
$box_content = $box_content->text("singleprod");

} else {
header("HTTP/1.1 301 Moved Permanently");
header("Location: {$installDir['value']}");
//$box_content->parse("noprod");
//$box_content = $box_content->text("noprod");
}

?>